<?php
namespace Home\Controller;

class ProductController extends \Home\Controller\BaseController
{

	//获取商品信息
	public function goodlist(){
		$param = I('params');
		$type = $param['type'];
		$area = $param['area'];
		$page = $param['page'];
		$size = $param['size'];

		$map['_string'] = ' area = "'.$area.'" OR area = "请选择城市"';
		$map['type'] = $type;
		$map['status'] = '1';
		
		$res = M("Good")
			->where($map)
			->field(array('title','sub_title','range','status','create_time','type','purchase_num','img_path','production_time','area','original_price','discount_price','stock','appointment',"end_time","id"))
			->order("create_time desc")
			->page($page, $size)
			->select();
		$return = array();
		foreach($res as $key=>$val){
			if($val['end_time']){
				$val['end_time'] = strtotime($val['end_time']);
			}else{
				unset($val['end_time']);
			}
			$return[$key] = $val;
			$return[$key]['original_price'] = floatval($val['original_price']);
			$return[$key]['discount_price'] = floatval($val['discount_price']);
			$return[$key]['create_time'] = strtotime($val['create_time']);
			$return[$key]['stock'] = intval($val['stock']);
			$img_path = explode(",",$val['img_path']);
			$val['img_path'] = array();
			if(is_array($img_path)){
				foreach($img_path as $ks=>$vs){
					if($vs == ""){
						continue;
					}
					$val['img_path'][] = "http://121.14.38.35".$vs;
				}
			}
			$return[$key]['img_path'] = $val['img_path'][0];
		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}
	//获取商品详情
	public function goodinfo(){
		$param = I("params");
		$id = $param['id'];

		//类别
		$return['categorylist'] = M("GoodCategory")->where(array("good_id"=>$id))->field(array("id","name","value"))->select();
		//所有商品下属的价格
		$pricelist = M("GoodPrice")->where(array("good_id"=>$id,"status"=>'1'))->field(array("id","category_id","original_price","discount_price","stock","code"))->select();
		//商品的详情
		$return['goodinfo'] = M("Good")->where(array("id"=>$id))->find();
		foreach($pricelist as $key=>$val){
			$return['pricelist'][$key]=$val;
			$return['pricelist'][$key]["original_price"] = floatval($val['original_price']);
			$return['pricelist'][$key]["discount_price"] = floatval($val['discount_price']);
			$return['pricelist'][$key]["category_id"] = explode(",",$val['category_id']);
		}
		$return['goodinfo']['stock'] = intval($return['goodinfo']['stock']);
		if($return['goodinfo']['end_time']){
			$return['goodinfo']['end_time'] = strtotime($return['goodinfo']['end_time']);
		}else{
			unset($return['goodinfo']['end_time']);
		}
		unset($return['goodinfo']['info']);
		$appcallbackurl = "http://121.14.38.35/Public/static/App/goodinfo".$id.".html";
		if(file_exists("./Public/static/App/goodinfo".$id.".html")){
			$return['goodinfo']['info'] = $appcallbackurl;
		}
		$return['goodinfo']['min_price'] = floatval($return['goodinfo']['min_price']);
		$img_path = explode(",",$return['goodinfo']['img_path']);
		$return['goodinfo']['img_path'] = array();
		if(is_array($img_path)){
			foreach($img_path as $key=>$val){
				if($val == ""){
					continue;
				}
				$return['goodinfo']['img_path'][] = "http://121.14.38.35".$val;
			}
		}

		$return['goodinfo']["original_price"] = floatval($return['goodinfo']['original_price']);
		$return['goodinfo']["discount_price"] = floatval($return['goodinfo']['discount_price']);
		$return['goodinfo']['total_count'] = intval("1028");
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}
	//关键字搜索
	public function searchbykey(){
		$param = I("params");
		$user_id = $this->user['userId'];
		$key_name = $param['key'];
		$searchtype = $param['search_type'];
		$sort = $param['sort'];
		$order = "";

		if($searchtype!=1){
			if($searchtype == 2){
				$goodinvest = array();
				$investlist = M("Invest")->where(array("status"=>"1"))->select();
				if(is_array($investlist)){
					foreach($investlist as $key=>$val){
						$good_info = json_decode($val['good_info'],true);
						if(!is_array($good_info)){
							$good_info = json_decode($good_info,true);
						}
						foreach($good_info as $k=>$v){
							$goodinvest[$v["good_id"]] += $v['count'];
						}
					}
				}
				if($sort == 1){
					asort($goodinvest);
				}elseif($sort == 2){
					arsort($goodinvest);
				}
				function sortArrByField(&$array, $field, $desc = false){
					$fieldArr = array();
					foreach ($array as $k => $v) {
						$fieldArr[$k] = $v[$field];
					}
					$sort = $desc == false ? SORT_ASC : SORT_DESC;
					array_multisort($fieldArr, $sort, $array);
				}
			}
			switch($searchtype){
				case 3://价格
					if($sort == 1){
						$order = "discount_price asc";
					}elseif($sort == 2){
						$order = "discount_price desc";
					}
					break;
				case 4://最新
					if($sort == 1){
						$order = "create_time asc";
					}elseif($sort == 2){
						$order = "create_time desc";
					}
					break;
			}
		}
		$where['title'] = array("like","%".$key_name."%");
		$where['status'] ='1';
		$Good = M("Good");
		if($order){
			$Good->order($order);
		}
		$goodinfo = $Good->where($where)->field(
			array("id","title",
				"sub_title",
				"status",
				"create_time",
				"type",
				"purchase_num",
				"img_path",
				"production_time",
				"area",
				"min_price",
				"appointment",
				"stock",
				"original_price",
				"discount_price",
				"end_time",
				"province",
				"city",
				"freeprovince",
				"postage",
				"supplier"))->select();


		$return = array();
		if(is_array($goodinfo)){
			foreach($goodinfo as $key=>$val){
				$img_path = explode(",",$val['img_path']);
				$val['img_path'] = "http://121.14.38.35".$img_path[0];
				$val['original_price'] = floatval($val['original_price']);
				$val['discount_price'] = floatval($val['discount_price']);
				$val['min_price'] = floatval($val['min_price']);
				$val['stock'] = intval($val['stock']);
				if($val['end_time']){
					$val['end_time'] = strtotime($val['end_time']);
				}else{
					unset($val['end_time']);
				}
				if(count($goodinvest[$val['id']])){
					$val['count'] = $goodinvest[$val['id']];
				}else{
					$val['count'] = "0";
				}
				$return[$key] = $val;
			}
			if($searchtype == 2){
				if($sort == 1){
					sortArrByField($return,"count",true);
				}else{
					sortArrByField($return,"count",false);
				}
			}

		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}
	//关键字搜索
	public function keylist(){
		$user_id = $this->user['userId'];
		$hotkey = global_config('hotkey');
		$this->ajaxReturn(array('error' => 0, 'info' => explode(",",$hotkey)));
	}
}
